import requests
import hashlib
import codecs
import base64


def test_ufm_token():
    def get_username():
        username = 'admin'
        password_bytes = username.encode('utf-8')
        base64_encoded_bytes = base64.b64encode(password_bytes)
        base64_username = base64_encoded_bytes.decode('utf-8')
        return base64_username

    login_url = 'http://10.0.11.99:8010/admin/auth/signin'
    login_data = {
        "username": get_username(),
        "password": [
            50,
            49,
            50,
            51,
            50,
            102,
            50,
            57,
            55,
            97,
            53,
            55,
            97,
            53,
            97,
            55,
            52,
            51,
            56,
            57,
            52,
            97,
            48,
            101,
            52,
            97,
            56,
            48,
            49,
            102,
            99,
            51
        ]
    }

    response = requests.post(url=login_url, json=login_data)
    response_json = response.json()
    return 'OCTSYSJA ' + response_json['accessToken']
    # return response_json


def md5_encrypt_to_bytes(pwd):
    pwd_str = str(pwd)
    m = hashlib.md5()
    m.update(pwd_str.encode('utf-8'))
    encrypted = m.hexdigest()
    return list(bytearray.fromhex(encrypted))


if __name__ == '__main__':
    token = test_ufm_token()
    print(token)
